'use strict';
const moment = require('moment');
const ulid = require('../public/ulid');
const { getCookieValue } = require('../public/processCookies');
module.exports = options => {
  return async function haha(ctx, next) {
    global.log = {};
    await next();
    if (global.log.desc) {
      const { id: tid } = getCookieValue(ctx.cookies, 'token', 'tenant');
      const { id: userId, name } = getCookieValue(ctx.cookies, 'token', 'user');
      await ctx.modelmysql.OperationLog.create({
        id: ulid.ulid(),
        nickname: name,
        userId,
        tid,
        operationBefore: global.log.before,
        operationAfter: global.log.after,
        desc: global.log.desc,
        date: moment(),
      });
    }
  };
};
